package com.framework.common.Enums;

import java.text.MessageFormat;


/**
 * 错误信息枚举
 */
public enum FrameworkErrorCodeEnum {

    SUCCESS("000000", "交易成功"),

    // 用户
    AUTHORIZATION_FAIL("900001", "认证失败!"),

    TOKEN_FAIL("900002", "令牌失效!"),

    REFRESH_TOKEN_FAIL("900003", "刷新令牌失败"),

    WXUSEROPENID_GET_FAIL("900004", "微信用户openid获取失败"),

    WXUSER_TOKEN_FAIL("900005", "微信token获取失败"),

    WXUSERINFO_GET_FAIL("900006", "微信用户信息获取失败"),

    USER_NOT_EXITS("900007", "用户信息不存在"),

    USER_HAS_ALREADY_CERT("900008","该用户已完成实名认证"),

    PICTURE_OCR_FAIL("900009","图片识别失败"),

    IDCARD_OCR_FAIL("900010","身份证识别不通过"),

    BUSINESS_OCR_FAIL("900011","营业执照识别不通过"),

    QUESTION_NOT_EXISTS("900012","试题不存在"),

    // 机构
    ORG_HAS_ALREADY_IN_SNAP("910001", "存在审核中或审核通过的机构信息"),

    // 通用
    PAPAM_IS_NULL("980001", "接口请求参数[对象]为空"),

    PARAM_ERROR("980002", "接口请求参数[{0}]为空,请先确认请求接口必要参数"),

    QUERY_RESULT_IS_EMPTY("980003", "查询结果记录为空"),

    END_DATE_EARLY_THAN_START_DATE("980004", "结束时间不能晚于开始时间"),

    FILE_READ_ERROR("980005", "文件读取异常"),

    FILE_UPLOAD_FAIL("980006", "文件上传失败"),

    FILE_NOT_EXISTS("980007", "文件不存在"),

    FILE_TOO_LARGE("980008", "上传文件大小超过限制"),

    NET_ERR("999996", "网络异常"),

    UNKOWN_ERR("999997", "未知错误"),

    FAIL("999998", "交易失败"),

    SYSTEM_ERR("999999", "系统异常"), ;

    private final String code;

    private final String msg;

    FrameworkErrorCodeEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 适用于含有占位符的枚举
     *
     * @param messageKeyArgs
     *            替换值
     * @return
     */
    public String getMsg(Object... messageKeyArgs) {
        return MessageFormat.format(this.msg, messageKeyArgs);
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public static FrameworkErrorCodeEnum getInstanceByCode(String code) {
        if (code == null || "".equals(code)) {
            return null;
        }
        for (FrameworkErrorCodeEnum instance : FrameworkErrorCodeEnum.values()) {
            if (instance.code.equals(code)) {
                return instance;
            }
        }
        return null;
    }
}
